package com.maxima.attendance.actions;

import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;

import com.maxima.attendance.constants.Constants;
import com.maxima.attendance.daos.ProjectDAO;
import com.maxima.attendance.pojos.Project;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ProjectAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private Integer id;
	private Map<String, Object> map = ActionContext.getContext().getSession();
	// for logging
	static Logger logger = Logger.getLogger(ProjectAction.class.getName());
	private ProjectDAO projectDAO = new ProjectDAO();

	public ProjectAction() {}

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}

	@Action(value = "addNewProject", results = {
	@Result(name = "success", type = "tiles", location = "adminHome.tiles"), @Result(name = "error", type = "tiles", location = "showAddProject.tiles")
	}, interceptorRefs = {
		@InterceptorRef(value = "adminStack")
	})
	public String addProject() {
		String projectName = ServletActionContext.getRequest().getParameter("project_name");
		String projectAgreement = ServletActionContext.getRequest().getParameter("project_agr");
		if (projectDAO.checkIfAlreadyPresent(projectAgreement)) {
			return ERROR;
		}
		Project project = new Project();
		project.setProjectName(projectName);
		project.setAgreement(projectAgreement);
		projectDAO.add(project);
		logger.info("New Project - " + project.getProjectName() + " Id - " + project.getId() + " - added Successfully");
		return SUCCESS;
	}

	@Action(value = "initEditProjectScreen", results = {
		@Result(name = "success", type = "tiles", location = "initEditProject.tiles")
	}, interceptorRefs = {
		@InterceptorRef(value = "adminStack")
	})
	public String selectProject() throws Exception {
		map.put(Constants.SELECTED_PROJECT, projectDAO.get(this.getId()));
		return SUCCESS;
	}

	@Action(value = "editProject", results = {
	@Result(name = "success", type = "tiles", location = "adminHome.tiles"), @Result(name = "error", type = "tiles", location = "showProject.tiles")
	}, interceptorRefs = {
		@InterceptorRef(value = "adminStack")
	})
	public String updateProject() throws Exception {
		Project project = (Project) this.getMap().get(Constants.SELECTED_PROJECT);
		String projectName = ServletActionContext.getRequest().getParameter("project_name");
		String projectAgreement = ServletActionContext.getRequest().getParameter("project_agr");
		if (projectDAO.checkIfAlreadyPresent(projectAgreement)) {
			return ERROR;
		}
		project.setProjectName(projectName);
		project.setAgreement(projectAgreement);
		projectDAO.update(project);
		logger.info("Holiday - " + project.getProjectName() + " Id - " + project.getId() + " - updated Successfully");
		map.put(Constants.SELECTED_PROJECT, null);
		return SUCCESS;
	}

	public String clearSelectedProject() {
		map.put(Constants.SELECTED_PROJECT, null);
		return SUCCESS;
	}
}
